package meforbio.wip.pint;

import java.util.Collection;
import java.util.HashSet;

public abstract class ProcessHitting {

	protected Collection<Sorte> sortes;
	protected Collection<Frappe> frappes;
	protected Collection<Cooperative> cooperativity;
	
	public abstract void generate();

	public static String addUnderscore(String cpd)
	{
		return "_"+cpd.replaceAll("[^0-9A-Za-z_]", "_");
	}
	
	@Override
	public String toString() {
		String result = "";
	System.out.println("#Sortes : " + sortes.size());
		for (Sorte s : this.sortes)
		{
			result = result + s.toString() + "\n";
		}
		System.out.println("#Frappes : " + frappes.size());
		result = result + "\n";
		for(Frappe f : this.frappes)
		{
			result = result + f.toString() + "\n";
		}
		result = result + "\n";
		System.out.println("#Frappes cooperatives : " + cooperativity.size());
		for(Cooperative c : this.cooperativity)
		{
			result = result + c.toString() + "\n";
		}
		
		return result;
	}

	
	public void remove(Sorte s)
	{
		Collection<Frappe> toRemoveF = new HashSet<Frappe>();
		for (Frappe f : this.frappes)
		{
			if(f.getSorteSrc().equals(s))
				toRemoveF.add(f);
			if(f.getSorteDest().equals(s))
				toRemoveF.add(f);
		}
		Collection<Cooperative> toRemoveC = new HashSet<Cooperative>();
		for (Cooperative f : this.cooperativity)
		{
			if(f.getSortesSrc().contains(s))
				toRemoveC.add(f);
			if(f.getSortesDest().contains(s))
				toRemoveC.add(f);
		}
		sortes.remove(s);
		frappes.removeAll(toRemoveF);
		cooperativity.removeAll(toRemoveC);
	}

	
	public Collection<Sorte> notConnectedSortes()
	{
		Collection<Sorte> result = new HashSet<Sorte>(sortes);
		result.removeAll(this.connectedSortes());
		return result;
	}
	
	public Collection<Sorte> connectedSortes()
	{
		Collection<Sorte> result = new HashSet<Sorte>();
		for (Frappe f : this.frappes)
		{
			result.add(f.getSorteSrc());
			result.add(f.getSorteDest());
		}
		for (Cooperative f : this.cooperativity)
		{
			result.addAll(f.getSortesSrc());
			result.addAll(f.getSortesDest());
		}
		return result;
	}
}
